package com.hog.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * List工具类
 *
 * @author 唐晴
 * @date 2020年08月05日
 * @since JDK 1.8
 */
public class ListUtil {

    public static <T> List<T> Collection2List(Collection<T> data) {
        List<T> list = new ArrayList<>();
        for (T d : data) {
            list.add(d);
        }
        return list;
    }

    public static boolean isEmpty(Collection<?> data) {
        return null == data || data.isEmpty();
    }

    public static boolean isNotEmpty(Collection<?> data) {
        return null != data && data.size() > 0;
    }

    /**
     * 对列表进行排序
     *
     * @param list   待排序列表
     * @param method 排序字段
     * @param sort   排序规则  desc:倒序   其他:正序
     * @param <E>
     */
    @SuppressWarnings("unchecked")
    public static <E> void sort(List<E> list, final String method, final String sort) {
        if (isEmpty(list)) {
            return;
        }
        Collections.sort(list, (E a, E b) -> {
            int ret = 0;
            try {
                Class<?>[] arrs = null;
                Object[] params = null;
                Method m1 = a.getClass().getMethod(method, arrs);
                Method m2 = b.getClass().getMethod(method, arrs);
                Object obj1 = m1.invoke(a, params);
                Object obj2 = m2.invoke(b, params);
                boolean desc = sort != null && "desc".equals(sort);
                if (null == obj1) {
                    if (null == obj2) {
                        return 0;
                    } else {
                        if (desc) {// 倒序
                            return 1;
                        } else {// 正序
                            return -1;
                        }
                    }
                } else if (null == obj2) {
                    if (desc) {// 倒序
                        return -1;
                    } else {// 正序
                        return 1;
                    }
                }
                if (obj1 instanceof Comparable) {
                    Comparable<Object> n1 = (Comparable<Object>) obj1;
                    Comparable<Object> n2 = (Comparable<Object>) obj2;
                    if (desc) {// 倒序
                        ret = n2.compareTo(n1);
                    } else {// 正序
                        ret = n1.compareTo(n2);
                    }
                } else {
                    String v1 = obj1.toString();
                    String v2 = obj2.toString();
                    if (desc) {// 倒序
                        ret = v2.compareTo(v1);
                    } else {// 正序
                        ret = v1.compareTo(v2);
                    }
                }
            } catch (NoSuchMethodException ne) {
                ne.printStackTrace();
            } catch (IllegalAccessException ie) {
                ie.printStackTrace();
            } catch (InvocationTargetException it) {
                it.printStackTrace();
            }
            return ret;
        });
    }
}
